package icraf.carbon.gui.model;

import icraf.carbon.core.Config.ConfigItem;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.LCTableModel.ValueType;
import icraf.carbon.gui.MatrixTableModel;
import icraf.carbon.gui.RowHighlight;
import icraf.carbon.model.ModelProject;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeListener;

public class ModelOutputPanel extends JPanel {
	private static final long serialVersionUID = 3935329706944049274L;

	private ModelProject modelProject;
	private ModelIterationSinglePanel netPanel = new ModelIterationSinglePanel();
	private RowHighlight rowHighlight = new RowHighlight() {
		@Override
		public Color getColor() {
			return new Color(230, 210, 250);
		}

		@Override
		public boolean isHighlight(MatrixTableModel table, int row, double f) {
			double precision = ConfigItem.PRECISION_MAX.getValueDouble();
			double precision10 = (float)Math.pow(10, precision);
			f = Math.round(f * precision10) / precision10;
			return f == 0;
		}
	};
	private JTabbedPane tabPanel;
	private ModelIterationSinglePanel totalNegPanel = new ModelIterationSinglePanel();
	private ModelIterationSinglePanel totalPosPanel = new ModelIterationSinglePanel();
	private ModelIterationPanel zonePanel = new ModelIterationPanel();

	public ModelOutputPanel() {
		super();
		tabPanel = new JTabbedPane();
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		zonePanel.setRowHighlight(true, rowHighlight);
		netPanel.setRowHighlight(true, rowHighlight);
		totalPosPanel.setRowHighlight(true, rowHighlight);
		totalNegPanel.setRowHighlight(true, rowHighlight);
	}

	public ModelProject getModelProject() {
		return modelProject;
	}

	private JPanel getPanel(int i, ModelProject modelProject, DataType dataType, Object attributes) {
		switch (i) {
		case 0:
			zonePanel.setModelProject(modelProject, dataType, attributes);
			return zonePanel;
		case 1:
			netPanel.setModelProject(modelProject, dataType, ValueType.TOTAL, attributes);
			return netPanel;
		case 2:
			totalPosPanel.setModelProject(modelProject, dataType,
					ValueType.TOTAL_POSITIVE, attributes);
			return totalPosPanel;
		case 3:
			totalNegPanel.setModelProject(modelProject, dataType,
					ValueType.TOTAL_NEGATIVE, attributes);
			return totalNegPanel;
		}
		return null;
	}

	public void setModelProject(ModelProject modelProject, DataType dataType,
			String[] tabLabel, Object attributes) {
		this.modelProject = modelProject;
		//TODO: mending bikin masing2 satu di extends;
		tabPanel.removeAll();
		for (int i = 0; i < tabLabel.length; i++) {
			tabPanel.addTab(tabLabel[i], getPanel(i, modelProject, dataType, attributes));
		}
	}
	
	public void setCornerSetting(String unitDef, String unitArea, boolean isUnitSelectable) {
		zonePanel.setCornerSetting( unitDef,  unitArea, isUnitSelectable);
		netPanel.setCornerSetting( unitDef,  unitArea, isUnitSelectable);
		totalNegPanel.setCornerSetting( unitDef,  unitArea, isUnitSelectable);
		totalPosPanel.setCornerSetting( unitDef,  unitArea, isUnitSelectable);
	}
	
	public void setZoneAreaScale(boolean isZoneAreaScale) {
		zonePanel.setZoneAreaScale(isZoneAreaScale);
		netPanel.setZoneAreaScale(isZoneAreaScale);
		totalNegPanel.setZoneAreaScale(isZoneAreaScale);
		totalPosPanel.setZoneAreaScale(isZoneAreaScale);
	}
	
	public void setSelectUnitArea(boolean b) {
		zonePanel.setSelectUnitArea(b);
		netPanel.setSelectUnitArea(b);
		totalNegPanel.setSelectUnitArea(b);
		totalPosPanel.setSelectUnitArea(b);
	}
	
	public void addZoneTabListener(ChangeListener l) {
		zonePanel.addZoneTabListener(l);
	}
	
	public void selectZoneTab(int i) {
		zonePanel.selectZoneTab(i);
	}

}